import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by admin on 2019/1/20.
 */
public class ThreadWatchDemo {
    /**
     * 核心线程池大小
     */
    static int corePoolSize = 10;
    /**
     * 线程池最大数量
     */
    static int maximumPoolSizeSize = 20;
    /**
     * 线程活动保持时间
     */
    static long keepAliveTime = 1;

    static AtomicInteger num = new AtomicInteger(0);
    /**
     * 任务队列
     */
    static ArrayBlockingQueue workQueue = new ArrayBlockingQueue(10);

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSizeSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                workQueue,
                new ThreadPoolExecutor.DiscardPolicy());
        //提交一个任务
        Runnable runnable = new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName() + "在执行任务" + num.addAndGet(1));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        for (int i = 0; i < 200; i ++) {
            executor.execute(runnable);
        }
        while (true) {
            System.out.println();

            int queueSize = executor.getQueue().size();
            System.out.println("当前排队线程数：" + queueSize);

            int activeCount = executor.getActiveCount();
            System.out.println("当前活动线程数：" + activeCount);

            int lag = executor.getLargestPoolSize();
            System.out.println("最大线程数：" + lag);    //会是多少？

            long completedTaskCount = executor.getCompletedTaskCount();
            System.out.println("执行完成线程数：" + completedTaskCount);

            long taskCount = executor.getTaskCount();
            System.out.println("总线程数：" + taskCount);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
